import db from '@dfeidao/atom-nodejs/db';
import dt from '@dfeidao/atom-nodejs/dt';
import log from '@dfeidao/atom-nodejs/logger/log';
import { IncomingHttpHeaders } from 'http';
import { systemid } from '../../atom/config';
interface Message {
	filter: {};
	page_no: number;
	page_size: number;
}

interface IWebResult {
	data: unknown;
	cookie?: {
		[name: string]: string;
	} | null;
	content_type?: string;
	headers?: {
		[key: string]: string;
	};
	attachment?: string;
	redirect?: string;
	status_code?: number;
}

export default async function atom(message: Message, action_id: string, session_id: string, headers: IncomingHttpHeaders): Promise<IWebResult> {
	log('Service begin path:operation-log/zj-000001/s001,action_id:' + action_id);


	const { exec, prepare } = db.query(action_id, session_id, systemid);
	const { exec: e, prepare: p } = db.count(action_id, session_id, systemid);

	prepare('operation_log', ['pub_user_no', 'resume_name', 'operate_type', 'op_cont', 'operation_time'], message.filter, message.page_size, message.page_no, [], ['operation_time']);

	const [data] = await exec();
	data.forEach((v: { operation_time: number; operation_time_str: string; }) => {
		v.operation_time_str = v.operation_time ? dt.format(v.operation_time, 'YYYY-MM-DD HH:mm') : '';
	});
	p('operation_log', message.filter);
	const [t] = await e();

	log('Service end path:operation-log/zj-000001/s001,action_id:' + action_id);

	return {
		data: { data, count: t }
	};
}
